package cn.nubia.admin.common.shiro.util;

import java.security.MessageDigest;

/**
 * Created by cnJason on 2015/7/13.
 */
public class SecretUtils {


    public SecretUtils() {
    }

    public static String md5Encrypt(String srcStr) {
        return encrypt("MD5", srcStr);
    }

    public static String sha1Encrypt(String srcStr) {
        return encrypt("SHA-1", srcStr);
    }

    public static String sha256Encrypt(String srcStr) {
        return encrypt("SHA-256", srcStr);
    }

    public static String sha384Encrypt(String srcStr) {
        return encrypt("SHA-384", srcStr);
    }

    public static String sha512Encrypt(String srcStr) {
        return encrypt("SHA-512", srcStr);
    }

    public static String encrypt(String algorithm, String srcStr) {
        try {
            StringBuilder e = new StringBuilder();
            MessageDigest md = MessageDigest.getInstance(algorithm);
            byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
            byte[] arr$ = bytes;
            int len$ = bytes.length;

            for (int i$ = 0; i$ < len$; ++i$) {
                byte b = arr$[i$];
                String hex = Integer.toHexString(b & 255);
                if (hex.length() == 1) {
                    e.append("0");
                }

                e.append(hex);
            }

            return e.toString();
        } catch (Exception var10) {
            throw new RuntimeException(var10);
        }
    }

}
